// pages/my/my.js
const app = getApp();
const util = require('../../utils/util.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    domain: app.api.xh.downloadDomain,
    teacherTagStyle: util.teacherTagStyles,
    default_avatar: '/image/teacher-default-avatar-male.png'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var logging = wx.getStorageSync('_logining');

    this.setData({
      logging: logging
    });
    
  },
  frmClick(e) {
    app.saveFormId(e.detail.formId);
  },
  getPersonalData() {
    let page = this;
    app.sendRequest({
      hideLoading: true,
      url: app.api.public.student_info,
      success: function(res) {
        let avatar = wx.getStorageSync('userInfo').avatarUrl;
        page.setData({
          user_info: res.data,
          default_avatar: avatar
        });
      }
    });
  },
  //  浏览过的课程
  getBrowsedCourseList: function() {
    var page = this;
    app.sendRequest({
      hideLoading: true,
      url: app.api.student.myself,
      data: {
        type: 3
      },
      success: function(res) {
        if (res.data != undefined && res.data != '' && res.data.list) {
          // 设置数据
          page.setData({
            browsed_courses: res.data.list
          });
        }
      }
    });
  },
  toPersonInfo: function(e) {
    let page = this;
    if (!page.data.logging) {
      app.toAuth('/pages/my/my');
    } else {
      // wx.navigateTo({
      //   url: '/pages/person_info/person_info'
      // });
    }
  },
  findMoreBrowsed: function(e) {
    let page = this;
    if (!page.data.logging) {
      app.toAuth('/pages/my/my');
    } else {
      wx.navigateTo({
        url: '/pages/browsed_course/browsed_course'
      });
    }
  },
  navTo: function(e) {
    var type = e.currentTarget.dataset.type,
      url = '';

    let page = this;
    if (!page.data.logging) {
      app.toAuth('/pages/my/my');
      return false;
    }

    switch (type) {
      case '1':
        // url = '/pages/order/order';
        url = '/pages/my_order/my_order';
        break;
      case '2':
        url = '/pages/my_course/my_course';
        break;
      case '3':
        url = '/pages/my_wrong_list/my_wrong_list';
        break;
      case '4':
        url = '/pages/my_coach/my_coach';
        break;
      default:
    }

    url != '' &&
      wx.navigateTo({
        url: url
      });
  },
  toFocus: function(e) {
    wx.navigateTo({
      url: '/pages/my_focus/my_focus'
    });
  },
  toCourse: function(e) {
    var cid = e.currentTarget.dataset.id;
    var curriculum_name = e.currentTarget.dataset.name;
    wx.navigateTo({
      url:
        '/pages/course_detail/course_detail?curriculum_id=' +
        cid +
        '&curriculum_name=' +
        curriculum_name
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (this.data.logging) {
      this.getPersonalData();
      this.getBrowsedCourseList();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {}
});
